/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.MappingServiceVersion;
import han.bifang.gwt.client.common.entity.VersionQos;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class RemoteVersionBasicContent extends Content {

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private ServiceMessages cons;

	private VerticalPanel panel = null;

	private RemoteVersionQosPanel vp;

	private TextBox version;

	private MappingServiceVersion v;

	private ArrayList<VersionQos> qos;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			Window.alert(cons.modifyOK());
			// String id = (String) res.getResponse();
			// HashMap p = new HashMap();
			// p.put("id", id);
			// listener.onContentChanged(
			// ServiceContentConstants.IMPORT_REMOTE_SERVICE_VERSION,
			// ServiceContentConstants.REMOTE_VERSION_ROOT, p);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public RemoteVersionBasicContent(ServiceMessages cons,
			BifangGWTServiceAsync service, GwtContext context,
			MappingServiceVersion v, ArrayList<VersionQos> qos) {
		this.cons = cons;
		this.context = context;
		this.service = service;
		this.v = v;
		this.qos = qos;

		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("100%");

		initWidget(panel);

	}

	public void onLoad() {
		Grid table = new Grid(1, 2);

		// table.setWidth("100%");
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);

		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "30%");
		cf.setWidth(1, "70%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");

		table.setHTML(0, 0, cons.version());

		version = new TextBox();
		version.setVisibleLength(32);
		version.setText(v.getVersion());
		table.setWidget(0, 1, version);
		panel.add(table);

		panel.add(new HTML("&nbsp;"));

		HTML h = new HTML(cons.configVersion());
		panel.add(h);

		vp = new RemoteVersionQosPanel(cons, context, qos);
		panel.add(vp);

		Button b = new Button(cons.modify());
		b.addClickListener(new ClickListener() {
			public void onClick(Widget send) {

				ArrayList versionqos = vp.getVersionQos();
				if (versionqos == null)
					return;

				v.setVersion(version.getText());
				ArrayList r = new ArrayList();
				r.add(v);
				r.add(versionqos);

				RequestObject req = new RequestObject();
				req
						.setAction("han.bifang.gwt.server.action.service.ModifyMappingVersionAction");
				req.setRequest(r);

				service.call(req, cb);

			}
		});

//		Button b1 = new Button(cons.remove());
//		b1.addClickListener(new ClickListener() {
//			public void onClick(Widget send) {
//				HashMap p = new HashMap();
//				p.put("id", v.getId());
//				p.put("version", v.getVersion());
//				listener.onContentChanged(
//						ServiceContentConstants.REMOTE_VERSION_ROOT,
//						ServiceContentConstants.REMOVE_REMOTE_VERSION, p);
//
//				 RequestObject req = new RequestObject();
//				 req
//				 .setAction("han.bifang.gwt.server.action.service.ModifyMappingVersionAction");
//				 req.setRequest(r);
//				
//				 service.call(req, cb);
//
//			}
//		});
		if (PrivilegChecker.hasPrivilege(context, null))
			panel.add(b);

		// panel.add(b);

	}

	private boolean validate() {
		if (!StringUtils.checkLenght(version.getText(), 1, 20)) {
			Window.alert(cons.lengthError(cons.version(), "1", "20"));
			return false;
		}

		return true;

	}

}
